package edu.upf.da.p2p.sm.server.manager;

import java.util.Hashtable;
import java.util.Map;

import edu.upf.da.p2p.sm.P2PMessage;
import edu.upf.da.p2p.sm.P2PMessageListener;
import edu.upf.da.p2p.sm.server.message.UserGranted;

public class UserStatusManager extends P2PMessageListener {

	private Map<String, String> smToP2P;

	public UserStatusManager(){
		smToP2P = new Hashtable<String, String>();
	}

	public boolean isGranted(String smUser){
		return smToP2P.containsKey(smUser);
	}

	public boolean isLogged(String p2pUser){
		return smToP2P.containsValue(p2pUser);
	}

	public void processMessage(P2PMessage message) {
		UserGranted ug = (UserGranted) message;
		if(ug.getP2PUser().equalsIgnoreCase("")){ //Un cliente desloguea
			smToP2P.remove(ug.getSMUser());
		}else{ //El cliente ha logueado
			smToP2P.put(ug.getSMUser(),ug.getP2PUser());
		}

	}

}
